#include "../mqserver/mq_queue.hpp"
#include <gtest/gtest.h>

mqserver::MsgQueueManagerPtr mqmp;

class QueueTest : public testing::Environment 
{
public:
    virtual void SetUp() override { 
        mqmp = std::make_shared<mqserver::MsgQueueManager>("./data/meta.db");
    }
    virtual void TearDown() override { /* exmp->clear(); */ std::cout << "clear finish!\n"; }
};

TEST(queue_test, insert_test)
{
    // std::unordered_map<std::string, std::string> map = {{"kv1", "va1"}};
    std::unordered_map<std::string, std::string> map;
    mqmp->declareQueue("mq1", true, false, false, map);
    mqmp->declareQueue("mq2", true, false, false, map);
    mqmp->declareQueue("mq3", true, false, false, map);
    mqmp->declareQueue("mq4", true, false, false, map);
    ASSERT_EQ(mqmp->size(), 4); 
}

TEST(queue_test, select_test)
{
    ASSERT_EQ(mqmp->exists("mq1"), true);
    ASSERT_EQ(mqmp->exists("mq2"), true);
    ASSERT_EQ(mqmp->exists("mq3"), true);
    ASSERT_EQ(mqmp->exists("mq4"), true);
    mqserver::MsgQueuePtr mqp = mqmp->selectQueue("mq3");
    ASSERT_NE(mqp.get(), nullptr);
    ASSERT_EQ(mqp->name_, "mq3");
    ASSERT_EQ(mqp->exclusive_, false);
    ASSERT_EQ(mqp->durable_, true);
    ASSERT_EQ(mqp->auto_delete_, false);
    ASSERT_EQ(mqp->getArgs(), std::string("kv1=va1&"));
}

TEST(queue_test, remove_test)
{
    mqmp->deleteQueue("mq3");
    mqserver::MsgQueuePtr mqp= mqmp->selectQueue("mq3");
    ASSERT_EQ(mqp.get(), nullptr);
    ASSERT_EQ(mqmp->exists("mq3"), false);
}


int main(int argc,char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new QueueTest());
    (void)RUN_ALL_TESTS();
    return 0;
}